﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExSugar.Dto;
using RuoVea.ExSugar.UnitOfWork;
using RuoVea.OmiApi.Config.Service;
using RuoVea.OmiApi.Config.Service.Dto;
using RuoVea.OmiConfig.Attributes;
using RuoVea.OmiConfig.Dto;
using RuoVea.OmiConfig.Language;

namespace RuoVea.OmiConfig.Controllers;
/// <summary>
/// 参数配置
/// </summary>
[AppRoute]
public class ConfigController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SysConfigService _sysConfigService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysConfigService"></param>
    public ConfigController(SysConfigService sysConfigService)
    {
        _sysConfigService = sysConfigService;
    }

    /// <summary>
    /// 参数配置页面
    /// </summary>
    [HttpGet(nameof(Index))]
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<SysConfigDto>>> GetPagesAsync(SysConfigPageParam data)
    {
        RestfulResult<PageResult<SysConfigDto>> restfulResult = new() { Code = CodeStatus.OK };
        try
        {
            data ??= new SysConfigPageParam { PageNo = 1, PageSize = 10 };
            restfulResult.Data = await _sysConfigService.GetPagesAsync(data);
        }
        catch (Exception ex)
        {
            restfulResult.Message = ex.Message;
            restfulResult.Code = CodeStatus.BadRequest;
        }
        return restfulResult;
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("data/{id:long}")]
    public async Task<RestfulResult<SysConfigDto>> GetDataAsync(long id)
    {
        var restful = new RestfulResult<SysConfigDto>() { Code = CodeStatus.BadRequest };
        if (id == 0)
        {
            restful.Message = i18n.getfail;
            return restful;
        }
        try
        {
            restful.Data = await _sysConfigService.GetDataAsync(new EntityBaseIdDto { Id = id });
            restful.Code = CodeStatus.OK;
        }
        catch (Exception ex)
        {
            restful.Message = ex.Message;
        }
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(SysConfigInputDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysConfigService.AddDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync(UpdateSysConfigInputDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            SysConfigInputDto sysConfigInputDto = new SysConfigInputDto
            {
                Id = data.Id,
                Name = data.Name,
                Code = data.Code,
                Value = data.Value,
                SysFlag = data.SysFlag,
                Remark = data.Remark,
                IsDisable = data.IsDisable,
                GroupCode = data.GroupCode,
                Sort = data.Sort,
            };
            await _sysConfigService.UpdateDataAsync(sysConfigInputDto);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteAsync(EntityBaseIdDto data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest, Data = false };

        if (data == null || data.Id == 0)
        {
            restful.Message = i18n.deletefail;
            return restful;
        }
        try
        {
            await _sysConfigService.DeleteDataAsync(data);
            restful.Data = true;
            restful.Code = CodeStatus.OK;
        }
        catch (Exception ex)
        {
            restful.Message = ex.Message;
        }
        return restful;
    }
}
